import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class Question2 {

    public static LocalDate convert(String old) {
        final String[] s = old.split(" ");
        int year = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);
        int wNum = Integer.parseInt(s[2]);
        int w = Integer.parseInt(s[3]);
        LocalDate localDate = LocalDate.of(year, m, 1);
        // 根据周数和周几来判断
        final DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        final int startValue = dayOfWeek.getValue();
        int dayOfMonth = (wNum - 1) * 7 + (w - startValue);
        // 判断时间是否超出了界限
        final LocalDate date = localDate.plusDays(dayOfMonth);
        if (date.getMonthValue() != m) {
            return null;
        }
        return date;
    }

    public static void main(String[] args) {
        final Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            final LocalDate convert = convert(scanner.nextLine());
            if (convert == null) {
                System.out.println("0");
            } else {
                System.out.println(convert.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            }
        }
    }

}
